﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.Preferences;
using ApplicationLibrary.SqlConnector;
using Plumber.Accounting.Preferences;
using Plumber.Masters.Library;
using Plumber.Masters.ModuleData;

namespace Plumber.Masters.Forms
{
    public partial class Company : DocumentForm
    {
        public ApplicationLibrary.ExtendedControls.TabControl masterTabControl { get { return tabControl1; } }

        public Company()
            : base(Properties.Resources.FN_Quotations)
        {
            InitializeComponent();
            keyControl = null;
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarCompanyParam(1);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerCompany("Company", new RadarCompanyForm(), this);
            dbManager.AddMaster(this, PL_Company.Name, true);

            rdrTitle.RadarForm = new RadarTitles();
            rdrTitle.DescriptionControl = lblTitleDesc;
        }

        protected override bool OnEnableToolbarButtons(ToolStripButton button)
        {
            if (button.Name == "tbnNew")
            {
                DRCountCompany countCompany = new DRCountCompany();
                return !countCompany.Find();
            }

            return base.OnEnableToolbarButtons(button);
        }

        protected override void OnBindData()
        {
            BindControl(txtCompany, PL_Company.Description, Findable.YES);
            BindControl(txtAddress, PL_Company.Address);
            BindControl(mtbZip, PL_Company.ZipCode);
            BindControl(txtCity, PL_Company.City);
            BindControl(txtCounty, PL_Company.County);
            BindControl(txtPhone1, PL_Company.Phone1);
            BindControl(txtPhone2, PL_Company.Phone2);
            BindControl(txtFax, PL_Company.Fax);
            BindControl(txtRefer1, PL_Company.Refer1);
            BindControl(txtRefer2, PL_Company.Refer2);
            BindControl(rdrTitle, PL_Company.Title);
            BindControl(txtEmail, PL_Company.Email);

            BindControl(ftbTaxNo, PL_Company.TaxNo);
            BindControl(ftbFiscalCode, PL_Company.FiscalCode);
        }

        protected override bool OnBeforeSave()
        {
            if (!ftbFiscalCode.CheckIsOK())
                return false;

            if (!ftbTaxNo.CheckIsOK())
                return false;

            return base.OnBeforeSave();
        }

        protected override void OnPrepareAuxData()
        {
            rdrTitle.UpdateDescription();
            base.OnPrepareAuxData();
        }

        protected override bool OnBeforeAddNew()
        {
            return base.OnBeforeAddNew();
        }

        protected override bool OnAfterAddNew()
        {
            return true;
        }

        protected override void FocusOnNew()
        {
            txtCompany.Focus();
        }

        protected override void OnDisableControlsForEdit()
        {
        }

        protected override void OnAddSplitMenuButton()
        {
            //AddSplitPreviewButton(Properties.Resources.PRNT_ENVELOPE, PRINT_ENVELOPE, PrintType.Envelope, Properties.Resources.B_Envelope);
            //AddSplitPrintButton(Properties.Resources.PRNT_ENVELOPE, PRINT_ENVELOPE, PrintType.Envelope, Properties.Resources.B_Envelope);
            //AddSplitPrintButton(Properties.Resources.SEND_MAIL, SEND_MAIL, PrintType.Email, Properties.Resources.B_Email);
        }

        protected override bool OnPrintDocument(PrintInfo sender, printerForm pf)
        {
            //switch (sender.ReportName)
            //{
            //    case SEND_MAIL:

            //        //return PrintInvoice(pf);
            //        break;

            //    case PRINT_ENVELOPE:
            //        PrintEnvelope printEnvelope = new PrintEnvelope(pf);
            //        printEnvelope.Print(lblTitleDesc.Text, txtCompany.Text, txtAddress.Text, mtbZip.Text, txtCity.Text, txtCounty.Text);
            //        break;

            //    default:
            //        return true;
            //}
            return true;
        }

        #endregion
    }

    #region dbmanager Company

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerCompany : DBManager
    {
        public dbmanagerCompany(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                if (e.Row[PL_Company.Code.Name] == System.DBNull.Value)
                    e.Row[PL_Company.Code.Name] = 1;
            }
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new PL_Company();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(PL_Company.Name);
            qB.AddCompare(PL_Company.Code, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam1 = new SqlABParameter("@p1", PL_Company.Code);
            sParam1.Value = 0;
            PList.Add(sParam1);

            return PList;
        }

        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            if (collection.Name == PL_Company.Name)
            {
                collection.Parameter[0].Value = key[0];
            }
        }
    }

    #endregion
}